package com.ruoyi.store.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.store.domain.WarehouseGBG;
import com.ruoyi.store.service.IWarehouseGoodsService;
import com.ruoyi.system.api.domain.store.WarehouseGoods;
import com.ruoyi.system.api.dto.WarehouseGoodsDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 库存信息Controller
 *
 * @author wl
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/warehouseGoods")
@Slf4j
public class WarehouseGoodsController extends BaseController {
    @Autowired
    private IWarehouseGoodsService warehouseGoodsService;

    /**
     * 查询库存信息列表
     */
    @RequiresPermissions("store:warehouseGoods:list")
    @GetMapping("/list")
    public TableDataInfo list(WarehouseGoodsDTO warehouseGoodsDTO) {
        log.info("WarehouseGoodsController list warehouseGoodsDTO:{}", warehouseGoodsDTO);
        TableDataInfo tableDataInfo = getDataTable(warehouseGoodsService.selectWarehouseGoodsList(warehouseGoodsDTO));
        log.info("GoodsTypeController list tableDataInfo:{} goodsType:{}", tableDataInfo, warehouseGoodsDTO);
        return tableDataInfo;
    }

    /**
     * 获取库存价值
     *
     * @return
     */
    @RequiresPermissions("store:warehouseGoods:query")
    @GetMapping("getAllPrice")
    public AjaxResult getAllPrice() {
        return success(warehouseGoodsService.getAllPrice());
    }

    /**
     * 查询盘点库存信息列表
     */
    @RequiresPermissions("store:warehouseGoods:list")
    @GetMapping("/takingList")
    public TableDataInfo takingList(WarehouseGoods warehouseGoods) {
        log.info("WarehouseGoodsController takingList WarehouseGoods:{}", warehouseGoods);
        TableDataInfo tableDataInfo = getDataTable(warehouseGoodsService.takingList(warehouseGoods));
        log.info("GoodsTypeController takingList tableDataInfo:{}", tableDataInfo);
        return tableDataInfo;
    }

    /**
     * @return com.ruoyi.common.core.domain.R<java.util.List < com.ruoyi.system.api.domain.warehouse.WarehouseGoods>>
     * @description 查询库存信息列表（不分页）
     * @date 2024/11/4 下午3:53
     * @Param warehouseGoodsDTO:
     **/
    //    @RequiresPermissions("store:warehouseGoods:list")
    @PostMapping("/getWarehouseGoodsList")
    public R<List<WarehouseGoods>> getWarehouseGoodsList(@RequestBody WarehouseGoodsDTO warehouseGoodsDTO) {
        log.info("WarehouseGoodsController getWarehouseGoodsList warehouseGoodsDTO:{}", warehouseGoodsDTO);
        return R.ok(warehouseGoodsService.selectWarehouseGoodsList(warehouseGoodsDTO));
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据商品和仓库查询库存
     * @date 2024/11/5 下午4:05
     * @Param goodsId:
     * @Param houseId:
     **/
    //    @RequiresPermissions("store:warehouseGoods:list")
    @GetMapping("/getStockByGoodsIdHouseId")
    public AjaxResult getStockByGoodsIdHouseId(Integer goodsId, Integer houseId) {
        log.info("WarehouseGoodsController getStockByGoodsIdHouseId goodsId:{},warehouseId:{}", goodsId, houseId);
        return warehouseGoodsService.getStockByGoodsIdHouseId(goodsId, houseId);
    }

    @GetMapping("/getStockByGoodsId")
    public R<Integer> getStockByGoodsId(Integer goodsId) {
        log.info("WarehouseGoodsController getStockByGoodsId goodsId:{}", goodsId);
        return R.ok(warehouseGoodsService.getStockByGoodsId(goodsId));
    }


    /**
     * 导出库存信息列表
     */
    @RequiresPermissions("store:warehouseGoods:export")
    @Log(title = "库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarehouseGoodsDTO warehouseGoodsDTO) {
        ExcelUtil util;
        if (warehouseGoodsDTO.getIsGroupByGoods() != null && warehouseGoodsDTO.getIsGroupByGoods()) {
            util = new ExcelUtil<WarehouseGBG>(WarehouseGBG.class);
        } else {
            util = new ExcelUtil<WarehouseGoods>(WarehouseGoods.class);
        }
        List list = warehouseGoodsService.selectWarehouseGoodsList(warehouseGoodsDTO);
        util.exportExcel(response, list, "库存信息数据");
    }

    /**
     * 获取库存信息详细信息
     */
    @RequiresPermissions("store:warehouseGoods:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        log.info("WarehouseGoodsController getInfo id:{}", id);
        AjaxResult ajaxResult = warehouseGoodsService.selectWarehouseGoodsById(id);
        log.info("WarehouseGoodsController getInfo ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 根据商品ID获取库存信息
     */
//    @RequiresPermissions("store:warehouseGoods:query")
    @GetMapping("/getInfoByGoodsId")
    public AjaxResult getInfoByGoodsId(Integer goodsId) {
        log.info("WarehouseGoodsController getInfoByGoodsId goodsId:{}", goodsId);
        AjaxResult ajaxResult = warehouseGoodsService.selectWarehouseGoodsByGoodsId(goodsId);
        log.info("WarehouseGoodsController getInfoByGoodsId ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 新增库存信息
     */
    @RequiresPermissions("store:warehouseGoods:add")
    @Log(title = "库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarehouseGoods warehouseGoods) {
        log.info("WarehouseGoodsController add warehouseGoods:{}", warehouseGoods);
        AjaxResult ajaxResult = warehouseGoodsService.insertWarehouseGoods(warehouseGoods);
        log.info("WarehouseGoodsController add ajaxResult:{} warehouseGoods: {}", ajaxResult, warehouseGoods);
        return ajaxResult;
    }

    /**
     * 修改库存信息
     */
    @RequiresPermissions("store:warehouseGoods:edit")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarehouseGoods warehouseGoods) {
        log.info("WarehouseGoodsController edit warehouseGoods:{}", warehouseGoods);
        AjaxResult ajaxResult = warehouseGoodsService.updateWarehouseGoods(warehouseGoods);
        log.info("WarehouseGoodsController edit ajaxResult:{} warehouseGoods: {}", ajaxResult, warehouseGoods);
        return ajaxResult;
    }

    /**
     * 删除库存信息
     */
    @RequiresPermissions("store:warehouseGoods:remove")
    @Log(title = "库存信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("WarehouseGoodsController remove ids:{}", Arrays.toString(ids));
        AjaxResult ajaxResult = warehouseGoodsService.deleteWarehouseGoodsByIds(ids);
        log.info("WarehouseGoodsController remove ajaxResult:{} ids: {}", ajaxResult, Arrays.toString(ids));
        return ajaxResult;
    }
}
